<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <!-- 引入组件库 -->
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
        <style>
            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }
            .datatable td, .datatable th {
                padding: 12px 0;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: left;
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>系统管理<small>菜单管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                    <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                        <el-button type="primary" class="butT" @click="openAll()" icon="el-icon-view">展开</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label=" ">
                            <template slot-scope="scope">
                                <div @click="treeClick(scope.row,scope.$index)" style="cursor: pointer;">
                                    <template v-if="scope.row.children && scope.row.children.length > 0">
                                        <i class="el-icon-arrow-down" :style="'margin-left:'+(scope.row.level-1)*2+'em;'" v-if="scope.row.open"></i>
                                        <i class="el-icon-arrow-right" :style="'margin-left:'+(scope.row.level-1)*2+'em;'" v-else></i>
                                        <span >{{ scope.row.date }}</span>
                                    </template>
                                    <span v-else :style="'margin-left:'+(scope.row.level-1)*2+'em;'">{{ scope.row.date }}</span>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                        <el-table-column prop="linkUrl" label="访问路径" align="center"></el-table-column>
                        <el-table-column prop="icon" label="图标" align="center"></el-table-column>
                        <el-table-column prop="path" label="路由" align="center"></el-table-column>
                        <el-table-column prop="priority" label="优先级" align="center"></el-table-column>
                        <el-table-column prop="description" label="说明" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <!--<div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>-->
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form ref="menuForm" :model="formData" label-position="right" label-width="100px" :rules="rules">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="上级菜单">
                                                        <template>
                                                            <el-select placeholder="请选择" v-model="formData.parentMenuId" @change="initForm">
                                                                <el-option :key="item.id" :label="item.name" :value="item.id" v-for="item in dataMenu"></el-option>
                                                            </el-select>
                                                        </template>
                                                    </el-form-item>
                                                </el-col>
                                                <!--<el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>-->
                                                <el-col :span="12">
                                                    <el-form-item label="菜单名称" prop="name">
                                                        <el-input v-model="formData.name" @blur="validateName()"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="访问路径">
                                                        <el-input v-model="formData.linkUrl"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="图标">
                                                        <template>
                                                            <el-select placeholder="请选择" v-model="formData.icon">
                                                                <el-option :key="item.id" :label="item.name" :value="item.id" v-for="item in iconList"><li :class="item.name">{{" "+item.name}}</li></el-option>
                                                            </el-select>
                                                        </template>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="路由">
                                                        <el-input v-model="formData.path" readonly></el-input>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="优先级">
                                                        <el-input v-model="formData.priority" readonly></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="描述">
                                                        <el-input v-model="formData.description" type="textarea"></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form ref="menuForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="上级菜单">
                                                        <template>
                                                            <el-select placeholder="请选择" v-model="formData.parentMenuId" @change="initForm">
                                                                <el-option :key="item.id" :label="item.name" :value="item.id" v-for="item in dataMenu"></el-option>
                                                            </el-select>
                                                        </template>
                                                    </el-form-item>
                                                </el-col>
                                                <!--<el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>-->
                                                <el-col :span="12">
                                                    <el-form-item label="菜单名称" prop="name">
                                                        <el-input v-model="formData.name" @blur="validateName2()"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="访问路径">
                                                        <el-input v-model="formData.linkUrl"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="图标">
                                                        <template>
                                                            <el-select placeholder="请选择" v-model="formData.icon">
                                                                <el-option :key="item.id" :label="item.name" :value="item.id" v-for="item in iconList"><li :class="item.name"></li></el-option>
                                                            </el-select>
                                                        </template>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="路由">
                                                        <el-input v-model="formData.path" readonly></el-input>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="优先级">
                                                        <el-input v-model="formData.priority" readonly></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="描述">
                                                        <el-input v-model="formData.description" type="textarea"></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>

    <script>
        //自定义验证
        var verification_flag;
        var checkName = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('名字不能为空'));
            }
            setTimeout(() => {
                if (!verification_flag) {
                    callback();
                }else {
                    callback(new Error('菜单名称重复'));
                }
            }, 1000);
        }

        //生成xcode值用于树形的关闭功能
        var util = {};
        util.treeTableXcode = function(data,xcode){
            xcode = xcode || "";
            for(var i=0;i<data.length;i++){
                var item = data[i];
                item.xcode = xcode + i;
                if(item.children && item.children.length > 0){
                    util.treeTableXcode(item.children,item.xcode+"-");
                    console.log(JSON.stringify(item.xcode))
                }
            }
        };

        var vue = new Vue({
            el: '#app',
            data:{
                activeName:'first',//添加/编辑窗口Tab标签名称
                pagination: {//分页相关属性
                    currentPage: 1,
                    pageSize:10,
                    total:100,
                    queryString:null,
                },
                openFlag:false,
                iconList:[
                    {id:'el-icon-info',name:'el-icon-info'},
                    {id:'el-icon-error',name:'el-icon-error'},
                    {id:'el-icon-success',name:'el-icon-success'},
                    {id:'el-icon-warning',name:'el-icon-warning'},
                    {id:'el-icon-question',name:'el-icon-question'},
                    {id:'el-icon-back',name:'el-icon-back'},
                    {id:'el-icon-arrow-left',name:'el-icon-arrow-left'},
                    {id:'el-icon-arrow-down',name:'el-icon-arrow-down'},
                    {id:'el-icon-arrow-right',name:'el-icon-arrow-right'},
                    {id:'el-icon-arrow-up',name:'el-icon-arrow-up'},
                    {id:'el-icon-caret-left',name:'el-icon-caret-left'},
                    {id:'el-icon-caret-bottom',name:'el-icon-caret-bottom'},
                    {id:'el-icon-caret-top',name:'el-icon-caret-top'},
                    {id:'el-icon-caret-right',name:'el-icon-caret-right'},
                    {id:'el-icon-d-arrow-left',name:'el-icon-d-arrow-left'},
                    {id:'el-icon-d-arrow-right',name:'el-icon-d-arrow-right'},
                    {id:'el-icon-minus',name:'el-icon-minus'},
                    {id:'el-icon-plus',name:'el-icon-plus'},
                    {id:'el-icon-remove',name:'el-icon-remove'},
                    {id:'el-icon-circle-plus',name:'el-icon-circle-plus'},
                    {id:'el-icon-remove-outline',name:'el-icon-remove-outline'},
                    {id:'el-icon-circle-plus-outline',name:'el-icon-circle-plus-outline'},
                    {id:'el-icon-close',name:'el-icon-close'},
                    {id:'el-icon-check',name:'el-icon-check'},
                    {id:'el-icon-circle-close',name:'el-icon-circle-close'},
                    {id:'el-icon-circle-check',name:'el-icon-circle-check'},
                    {id:'el-icon-circle-close-outline',name:'el-icon-circle-close-outline'},
                    {id:'el-icon-circle-check-outline',name:'el-icon-circle-check-outline'},
                    {id:'el-icon-zoom-out',name:'el-icon-zoom-out'},
                    {id:'el-icon-zoom-in',name:'el-icon-zoom-in'},
                    {id:'el-icon-d-caret',name:'el-icon-d-caret'},
                    {id:'el-icon-sort',name:'el-icon-sort'},
                    {id:'el-icon-sort-down',name:'el-icon-sort-down'},
                    {id:'el-icon-sort-up',name:'el-icon-sort-up'},
                    {id:'el-icon-tickets',name:'el-icon-tickets'},
                    {id:'el-icon-document',name:'el-icon-document'},
                    {id:'el-icon-goods',name:'el-icon-goods'},
                    {id:'el-icon-sold-out',name:'el-icon-sold-out'},
                    {id:'el-icon-news',name:'el-icon-news'},
                    {id:'el-icon-message',name:'el-icon-message'},
                    {id:'el-icon-date',name:'el-icon-date'},
                    {id:'el-icon-printer',name:'el-icon-printer'},
                    {id:'el-icon-time',name:'el-icon-time'},
                    {id:'el-icon-bell',name:'el-icon-bell'},
                    {id:'el-icon-mobile-phone',name:'el-icon-mobile-phone'},
                    {id:'el-icon-service',name:'el-icon-service'},
                    {id:'el-icon-view',name:'el-icon-view'},
                    {id:'el-icon-menu',name:'el-icon-menu'},
                    {id:'el-icon-more',name:'el-icon-more'},
                    {id:'el-icon-more-outline',name:'el-icon-more-outline'},
                    {id:'el-icon-star-on',name:'el-icon-star-on'},
                    {id:'el-icon-star-off',name:'el-icon-star-off'},
                    {id:'el-icon-location',name:'el-icon-location'},
                    {id:'el-icon-location-outline',name:'el-icon-location-outline'},
                    {id:'el-icon-phone',name:'el-icon-phone'},
                    {id:'el-icon-phone-outline',name:'el-icon-phone-outline'},
                    {id:'el-icon-picture',name:'el-icon-picture'},
                    {id:'el-icon-picture-outline',name:'el-icon-picture-outline'},
                    {id:'el-icon-delete',name:'el-icon-delete'},
                    {id:'el-icon-search',name:'el-icon-search'},
                    {id:'el-icon-edit',name:'el-icon-edit'},
                    {id:'el-icon-edit-outline',name:'el-icon-edit-outline'},
                    {id:'el-icon-rank',name:'el-icon-rank'},
                    {id:'el-icon-refresh',name:'el-icon-refresh'},
                    {id:'el-icon-share',name:'el-icon-share'},
                    {id:'el-icon-setting',name:'el-icon-setting'},
                    {id:'el-icon-upload',name:'el-icon-upload'},
                    {id:'el-icon-upload2',name:'el-icon-upload2'},
                    {id:'el-icon-download',name:'el-icon-download'},
                    {id:'el-icon-loading',name:'el-icon-loading'}
                ],
                rules: {//校验规则
                    // name: [{required: true, message: '权限名称为必填项', trigger: 'blur'}],
                    name: [{ validator: checkName, trigger: 'blur' }]
                },
                oldName:'',
                dataMenu: [],
                dataList: [],//列表数据
                formData: {},//表单数据
                tableData:[],//新增和编辑表单中对应的检查项列表数据
                checkitemIds:[],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
                dialogFormVisible: false,//控制添加窗口显示/隐藏
                dialogFormVisible4Edit:false//控制编辑窗口显示/隐藏
            },
            created() {
                this.findPage();
            },
            methods: {
                //编辑
                handleEdit() {
                    axios.post("/menu/edit.do",this.formData).then(res=>{
                        this.resetForm();
                        //弹出窗口
                        this.dialogFormVisible4Edit = false;
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success':'error'
                        });
                        this.findPage();//分页查询
                    })
                },
                //添加
                handleAdd () {
                    //验证表单（省略了。。。）
                    this.$refs["menuForm"].validate((valid) => {
                        if (valid) {
                            //提交表单数据  提交检查组(checkgroup)以及勾选检查项ids Integer[] checkItemIds
                            axios.post("/menu/add.do",this.formData).then(res=>{
                                this.resetForm();
                                //弹出窗口
                                this.dialogFormVisible = false;
                                this.$message({
                                    message: res.data.message,
                                    type: res.data.flag ? 'success':'error'
                                });
                                this.findPage();//分页查询
                            })
                        }
                    });
                },
                //分页查询
                findPage() {
                    var parms = {
                        queryString:this.pagination.queryString
                    }
                    //根据条件查询所有菜单
                    axios.post("/menu/findByCondition.do",parms).then(res=>{
                        //后台返回的是PageResult total rows
                        if (res.data.flag){
                            this.dataList = res.data.data;
                            util.treeTableXcode(this.dataList);
                        } else {
                            this.$message.error(res.data.message);
                        }

                    })
                },
                // 重置表单
                resetForm() {
                    this.formData = {};
                    this.checkitemIds = [];
                    this.activeName ='first';
                },
                // 弹出添加窗口
                handleCreate() {
                    //清空表单数据 以及 默认选中第一个选项卡
                    this.resetForm();
                    this.dialogFormVisible = true;
                    //回显父菜单列表
                    axios.get("/menu/findParentMenuList.do").then(res=>{
                        //res.data;//Result对象
                        if(res.data.flag){
                            //赋值列表
                            this.dataMenu = res.data.data;
                            //初始化路由和优先级
                            this.formData.path = this.dataMenu.length+2;
                            this.formData.priority = this.dataMenu.length+1;
                        }
                        else {
                            this.$message.error(res.data.message);
                        }
                    })
                },
                // 弹出编辑菜单窗口
                handleUpdate(row) {
                    //弹出编辑窗口
                    this.dialogFormVisible4Edit = true;
                    // 并回显菜单
                    axios.get("/menu/findById.do?id="+row.id).then(res=>{
                        this.formData = res.data.data;//回显检查组数据
                        this.oldName = this.formData.name;
                        //查询父菜单列表进行回显
                        axios.get("/menu/findParentMenuList.do").then(res=>{
                            //res.data;//Result对象
                            if(res.data.flag){
                                this.dataMenu = res.data.data;
                            }
                            else {
                                this.$message.error(res.data.message);

                            }
                        })
                    })
                },
                //切换页码
                /*handleCurrentChange(currentPage) {
                    this.pagination.currentPage = currentPage;
                    this.findPage();
                },*/
                // 删除菜单
                handleDelete(row) {
                    this.$confirm('此操作将永久删除菜单记录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        //发送删除请求
                        axios.get("/menu/deleteById.do?id="+row.id).then(res=>{
                            if (res.data.flag){
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                                this.findPage();//分页查询
                            } else {
                                this.$message.error(res.data.message);
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                },
                validateName(){
                    var param = {
                        menuName : this.formData.name
                    }
                    axios.post("/menu/validateName.do",param).then(res=>{
                        verification_flag = res.data.flag;
                    })
                },
                validateName2(){
                    console.log(JSON.stringify(this.oldName))
                    console.log(JSON.stringify(this.formData.name))
                    if (this.oldName != this.formData.name) {
                        this.validateName();
                    }
                },
                ////页面菜单树形点击事件
                treeClick:function(item,index){
                    if(item.open){
                        this.collapse(item,index);
                    }else{
                        this.expand(item,index);
                    }
                },
                //页面菜单树形展开
                expand:function(item,index){
                    if(!item.children){
                        return index;
                    }
                    //展开
                    for(var i=0;item.children && i<item.children.length;i++){
                        var child = item.children[i];
                        this.dataList.splice(++index,0,child);
                        if(child.children && child.children.length > 0 && child.open){
                            index = this.expand(child,index);
                        }
                    }
                    item.open = true;
                    return index;
                },
                //页面菜单树形关闭
                collapse:function(item,index){
                    if(!item.children){
                        return index;
                    }
                    //收缩
                    item.open = false;
                    var len = 0;
                    for(var i=index+1;i<this.dataList.length;i++){
                        var xcode = this.dataList[i].xcode;
                        console.log(JSON.stringify(xcode))
                        if(xcode.startsWith(item.xcode+"-")){
                            len ++;
                        }else{
                            break;
                        }
                    }
                    this.dataList.splice(index+1,len);
                },
                //全部展开
                openAll(){
                    if (this.openFlag) {
                        this.openFlag = false;
                        for (var i = 0; i < this.dataList.length; i++) {
                            if (this.dataList[i].level = 1) {
                                this.collapse(this.dataList[i],i)
                            }
                        }
                    }else {
                        this.openFlag = true;
                        for (var i = 0; i < this.dataList.length; i++) {
                            if (this.dataList[i].level = 1) {
                                this.expand(this.dataList[i],i)
                            }
                        }
                    }
                },
                handleChange(value) {
                    console.log(value);
                },
                //根据父菜单的选择进行表单关联项回显
                initForm(val){
                    if (val == 0){
                        this.formData.path = this.dataMenu.length+2;
                        this.formData.priority = this.dataMenu.length+1;
                    }else {
                        for (var i = 0; i < this.dataList.length; i++) {
                            if (this.dataList[i].id == val){
                                this.formData.path = "/"+this.dataList[i].path+"-"+(this.dataList[i].children.length+1);
                                this.formData.priority = this.dataList[i].children.length+1;
                            }
                        }
                    }
                }
            }
        })
    </script>
</html>
